/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entities;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author JackOddie
 */
public class Studio {

    private String id_studio;
    private String jenis_studio;
    private String harga_sewa_studio;
    private String detail_studio;
    private String studio_status;

    public Studio(String jenisStudio, String hargaStudio, String detailStudio, String studioStatus) {
        this.setHarga_sewa_studio(hargaStudio);
        this.setJenis_studio(jenisStudio);
        this.setDetail_studio(detailStudio);
        this.setStudio_status(studioStatus);
    }

    public Studio() {
    }
    
    public String getId_studio() {
        return id_studio;
    }
    public static int getJumlahStudio() throws SQLException{
        int x=0;
        DBConnection conn = new DBConnection();
        String query = "SELECT count(ID_STUDIO) FROM STUDIO";
        ResultSet rset = conn.select(query);
        if (rset.next() == false) {
            x=rset.getInt(0);
        }
        return x;
    }
//    public String autoGenerateIdStudio() throws SQLException {
//        int id =getJumlahStudio()+1;
//        String newID = "Studio"+String.valueOf(id);
//        return newID;
//    }
    public void saveToDB() throws SQLException{
        DBConnection db = new DBConnection();
        ResultSet rset = db.count("sum", "studio");
        int x=0;
        while(rset.next()){
            x=(int)rset.getDouble("sum");
        }
        String query = "insert into STUDIO values('"
                +"studio"+x+"','"
                +jenis_studio+"','"
                +harga_sewa_studio+"','"
                +detail_studio+"','"+studio_status+"')";
        
        db.updateData(query);
    }

    public static boolean isExist(String id) throws SQLException {
        DBConnection conn = new DBConnection();

        String query = "select * from STUDIO where ID_STUDIO='" + id +"'";
        ResultSet rset = conn.select(query);
        if (rset.next() == false) {
            return false;
        }
        return true;
    }

    public static boolean isHargaSewaValid(String harga) {
        if (harga.length()>4 && harga.matches("[0-9]*")) {
            return true;
        } else {
            return false;
        }
    }
    
    public void setId_studio(String id_studio) {
        this.id_studio = id_studio;
    }

    public String getJenis_studio() {
        return jenis_studio;
    }

    public void setJenis_studio(String jenis_studio) {
        this.jenis_studio = jenis_studio;
    }

    public String getHarga_sewa_studio() {
        return harga_sewa_studio;
    }

    public void setHarga_sewa_studio(String harga_sewa_studio) {
        this.harga_sewa_studio = harga_sewa_studio;
    }

    public String getDetail_studio() {
        return detail_studio;
    }

    public void setDetail_studio(String detail_studio) {
        this.detail_studio = detail_studio;
    }
    public void getDataById(String id_studio) throws SQLException {
        DBConnection conn=new DBConnection();
        
        String query = "select * from STUDIO where ID_STUDIO='" +id_studio +  "'";
        ResultSet rset = conn.select(query);
        while (rset.next()) {
            this.id_studio = rset.getString("ID_STUDIO");
            jenis_studio = rset.getString("JENIS_STUDIO");
            harga_sewa_studio = rset.getString("HARGA_SEWA_STUDIO");
            detail_studio = rset.getString("DETAIL_STUDIO");
            
        }
    }
    public void setInactive() throws SQLException{
           DBConnection conn = new DBConnection();
        String query = "update studio set studio_status='inactive' where ID_STUDIO='"+id_studio+"'" ;
        conn.updateData(query);
    }
    public void setActive() throws SQLException{
           DBConnection conn = new DBConnection();
        String query = "update studio set studio_status='active' where ID_STUDIO='"+id_studio+"'" ;
        conn.updateData(query);
    }
    
    public void updateData() throws SQLException{
        DBConnection conn = new DBConnection();
        String query="update studio set HARGA_SEWA_STUDIO='"+harga_sewa_studio+"', JENIS_STUDIO='"+jenis_studio+"', DETAIL_STUDIO='"+detail_studio+"', STUDIO_STATUS='"+studio_status+"'where ID_STUDIO='"+id_studio+"'";
        conn.updateData(query);
    }

    public String getStudio_status() {
        return studio_status;
    }

    public void setStudio_status(String studio_status) {
        this.studio_status = studio_status;
    }

}
